﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Projet_IMA.Environnement;
using System.Drawing;
using Projet_IMA.Base;
using Projet_IMA.Habillage.Material;

namespace Projet_IMA.Object3D
{
    public class Spheres : Object3DBase
    {
        private V3 centre;
        private float rayon;

        public Spheres(MaterialBase material, V3 centre, float rayon, float pas)
            : base(pas, material)
        {
            this.centre = centre;
            this.rayon = rayon;
        }

        public override V3 getNormalOfPoint(V3 point)
        {
            V3 normal = point - centre;
            normal.Normalize();

            return normal;
        }

        protected override void MakeCache()
        {
            float nu = 2 * MF.PI, nv = MF.PI / 2;
            V3 point, duPoint, dvPoint;
            List<V3> points = new List<V3>((int)(2 * MF.PI / pas * MF.PI / pas));
            List<Color> c = new List<Color>((int)(2 * MF.PI / pas * MF.PI / pas));

            for (float u = 0; u < nu; u += pas)  // echantillonage fnt paramétrique
                for (float v = -MF.PI / 2; v < nv; v += pas)
                {
                    point = new V3(centre.x + rayon * MF.Cos(u) * MF.Cos(v), centre.y + rayon * MF.Sin(u) * MF.Cos(v), centre.z + rayon * MF.Sin(v));
                    points.Add(point);

                    duPoint = new V3(centre.x + rayon * -MF.Sin(u) * MF.Cos(v), centre.y + rayon * MF.Cos(u) * MF.Cos(v), centre.z + rayon * MF.Sin(v));
                    dvPoint = new V3(centre.x + rayon * MF.Cos(u) * -MF.Sin(v), centre.y + rayon * MF.Sin(u) * -MF.Sin(v), centre.z + rayon * MF.Cos(v));

                    c.Add(material.GetColor(u / nu, (v + MF.PI) / (nv + MF.PI), getNormalOfPoint(point), duPoint, dvPoint));
                }

            this.cache.Reset();
            cache.load(points, c);
        }
    }
}
